
/*----------------------------------------------------------------------------
------------------------------------------------------------------------------
Replication code for the analyses in: 

Huijsmans, T. & Rodden, J. (2024) The Great Global Divider? A Comparison of 
Urban-Rural Partisan Polarization in Western Democracies. Comparative Political
Studies.

Steps:

1. 	Download the URED_regions.csv, URED_elections.csv and URED_parties.csv from:
	https://osf.io/gwumr/files/osfstorage

2. 	Save the datasets in the same folder and write the location of this folder 
	in the global 'path' below here
	
*/

* set  path 
global path "[WRITE THE LOCATION WHERE YOU SAVE YOUR DATA HERE]"

	*EXAMPLE:
	* global path "C:\Users\Documents\URED_project\data"

/*

3. 	Run the code below

------------------------------------------------------------------------------
-----------------------------------------------------------------------------*/	

* Import datasets and save as .dta 

import delimited "$path\URED_elections.csv"	, clear 
	save URED_elections.dta, replace
import delimited "$path\URED_parties.csv"	, clear 
	save URED_parties.dta, replace

/* Note: 
The region-level dataset is only needed for replication of Appendix 1.1. Since
the file is quite large, it can take a lot of time to import and save. 
It is therefore only imported and saved when it is needed (see line 201)	
*/
	
**********************************************************************
**************************	Main analysis	**************************
**********************************************************************

cd "$path"
use URED_elections, clear


/*****************************************************************************
Figure 1. Urban rural electoral divides by country over time, with smoothed 
lines in one graph (left-hand larger graph) and underlying measures for each 
country in separate  (right-hand smaller )
******************************************************************************/


* drop UK observations from the 60s because of the gap before '92
replace country_code = "UK60" if country_code=="UK" & year<1990

keep if country_code == "GE2" | country_code == "IT" | country_code == "NL" | country_code == "ES" | country_code == "SE" | country_code == "UK" | country_code == "FR" | country_code == "CH" | country_code == "AUS" | country_code == "CA" | country_code == "US_cong" | country_code == "NO" | country_code == "NZ" | country_code == "DK" | country_code=="AT"

replace country_code="GE" if country_code=="GE2" //  = PR vote
replace country="Germany" if country=="Germany2" //  = PR vote

label variable country "country"
encode country, gen(id)   // convert string to numeric
order id                  // reorder the variable columns


* identify the most recent election in each country, for labels in 
gen tick = .
levelsof country_code, local(levels)
foreach l of local levels { 
sum year if country_code=="`l'"
replace tick = 1 if year == `r(max)' & country_code=="`l'"
}



* Declare the data as a panel

xtset id year              



* Generate smoothed trends in urban-rural electoral divide for each country
  
gen sm_ur_pol4=.
levelsof country_code, local(levels)
foreach l of local levels { 
    lowess  tot_ur_pol4 year if country_code=="`l'", gen(temp`l') nograph
    replace sm_ur_pol4 =temp`l'  if country_code=="`l'"
    drop temp`l'
}
  
  
  
* Draw graph of smoothed urban-rural electoral divide trends for all countries 
  
net install cleanplots, from("https://tdmize.github.io/data/cleanplots")  
summ year
local start = `r(min)'
local end   = `r(max)' + 5
display `end'
 xtline sm_ur_pol4 ///
 , overlay ///
  addplot( ///
 (scatter sm_ur_pol4 year if tick==1 & country_code!="AUS"  & country_code!="NO" & country_code!="UK" & country_code!="NZ"  & country_code!="NL"  & country_code!="CH", mcolor(black) msymbol(point) mlabel(country) mlabsize(vsmall) mlabcolor(black))   ///
 (scatter sm_ur_pol4 year if tick==1 & (country_code=="AUS" | country_code=="UK" | country_code=="NZ" ), mcolor(black) msymbol(point) mlabel(country) mlabsize(vsmall) mlabcolor(black) mlabang(20) mlabpos(2))     ///
 (scatter sm_ur_pol4 year if tick==1 & country_code=="NO" , mcolor(black) msymbol(point) mlabel(country) mlabsize(vsmall) mlabcolor(black) mlabang(40) mlabpos(12))     ///
 (scatter sm_ur_pol4 year if tick==1 & country_code=="NL", mcolor(black) msymbol(point) mlabel(country) mlabsize(vsmall) mlabcolor(black)  mlabang(30) mlabpos(6))     ///
 (scatter sm_ur_pol4 year if tick==1 & country_code=="CH", mcolor(black) msymbol(point) mlabel(country) mlabsize(vsmall) mlabcolor(black) mlabang(20) mlabpos(2)))     ///
   ttitle("") ///
  tlabel(`start'(5)2020, labsize(small) angle(vertical) grid) ///
  title() ///
  legend(off) ///
  graphregion(fcolor(white)) ///
  scheme(cleanplots) ///
  plot7opts(lcolor(black)) ///
  plot8opts(lcolor(dkorange)) ///
  plot14opts(lcolor(blue)) ///
  plot15opts(lcolor(red)) ///
  yscale(range(0(.05).3) fextend lcolor(black)) ///
  tscale(range(`start'(5)`end') fextend lcolor(black)) ///
  ytitle(Urban-rural divide) ///
  name(urdiv_low, replace) ///
  note(Smoothed trends)

  
* Draw separate  for each country, with the non-smoothed trends

gen country_str = subinstr(country, " ", "", .)


summ year
local start = `r(min)'
local end   = `r(max)'
levelsof country_str, local(levels)
foreach l of local levels {
xtline tot_ur_pol4 ///
 , overlay ///
  ttitle("") ///
  tlabel(`start'(5)`end', labsize(small) angle(45) grid) ///
  title("`l'") ///
  legend(off) ///
  graphregion(fcolor(white)) ///
  scheme(cleanplots) ///
  plot1opts(lcolor(black%10) lpattern(solid)) ///
  plot2opts(lcolor(black%10) lpattern(solid)) ///
  plot3opts(lcolor(black%10) lpattern(solid)) ///
  plot4opts(lcolor(black%10) lpattern(solid)) ///
  plot5opts(lcolor(black%10) lpattern(solid)) ///
  plot6opts(lcolor(black%10) lpattern(solid)) ///
  plot7opts(lcolor(black%10) lpattern(solid)) ///
  plot8opts(lcolor(black%10) lpattern(solid)) ///
  plot9opts(lcolor(black%10) lpattern(solid)) ///
  plot10opts(lcolor(black%10) lpattern(solid)) ///
  plot11opts(lcolor(black%10) lpattern(solid)) ///
  plot12opts(lcolor(black%10) lpattern(solid)) ///
  plot13opts(lcolor(black%10) lpattern(solid)) ///
  plot14opts(lcolor(black%10) lpattern(solid)) ///
  plot15opts(lcolor(black%10) lpattern(solid)) ///
      addplot(	(line tot_ur_pol4 year if country_str=="`l'", color(red) yscale(range(0(.05).35))) ) ///
  name(plot_`l', replace) ///
 yscale(range(0(.05).33) extend  lcolor(black)) ///
 ylab(0(.05).30) ///
  tscale(range(`start'(5)`end') fextend lcolor(black)) ///
  ytitle("")  
  }

graph combine plot_Austria plot_Australia plot_Canada plot_Switzerland plot_Denmark plot_Spain plot_France plot_Germany plot_Italy plot_Netherlands plot_Norway plot_NewZealand plot_Sweden plot_UnitedKingdom plot_UnitedStates, scheme(cleanplots) name(urdiv_con2, replace) imargin(vsmall) xsize(8) ysize(8)

 graph combine urdiv_low urdiv_con2, col(2)   scheme(cleanplots) xsize(8) ///
 title("Trends in urban-rural divides by country") 


graph export "$path\Figure1.pdf", as(pdf) name(Graph) replace 
graph export "$path\Figure1.svg", as(svg) name(Graph) replace width(1500) height(750)


/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

For the party-level plots in Figure 2, 3 and 4 of the paper, please use 
the r-script 'plots_parties_countries' that can be found at the OSF-page

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/



**********************************************************************
**************************		Appendices 	**************************
**********************************************************************

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

NOTE: 	To create Appendix 1.1, you need the region-level data file. This file 
		is quite large in size, so importing it, and working in it can take
		some time, and will potentiallly slow your computer while the data is 
		open and Stata is working in it. For replicating the rest of the 
		Appendices, you won't need this large region-level data-file again.
		
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

* Appendix 1.1: Comparison of absolute population density between deciles over time

import delimited "$path\URED_elections.csv"	, clear 
	save URED_elections.dta, replace
	
use "$path\URED_regions.dta", clear 


* Absolute max levels of population density by decile over time

bysort country_code year urban10: egen max_popdens = max(popdens)
egen pickone_cyd = tag(country_code year urban10)



local c_inc "AT AUS CA CH DK ES FR GE IT NL NO NZ SE UK US_cong"
foreach l of local c_inc {
twoway 	 ///
	(line max_popdens year if pickone_cyd & country_code=="`l'" & urban10==2, msymbol(none) lpattern(shortdash) lcolor(black%50)) ///
	(line max_popdens  year if pickone_cyd & country_code=="`l'" & urban10==4, msymbol(none) lpattern(shortdash) lcolor(black))	///
	(line max_popdens  year if pickone_cyd & country_code=="`l'" & urban10==6, msymbol(none) lpattern(longdash) lcolor(black)) ///
	(line max_popdens  year if pickone_cyd & country_code=="`l'" & urban10==8, msymbol(none) lpattern(longdash) lcolor(black%50)) ///
	, legend(col(4) title("Density decile cutoff") ///
				label(1 "2") ///
				label(2 "4")  ///
				label(3 "6") ///
				label(4 "8") ) ///
		scheme(s1mono) name(`l'_max_popdens, replace) ///
		title("`l'") ytitle(Population density ) xtitle("")
		graph save `l'_max_popdens.gph, replace
}



grc1leg 	"$path\AT_max_popdens.gph" ///
			"$path\AUS_max_popdens.gph" ///
			"$path\CA_max_popdens.gph" ///
			"$path\CH_max_popdens.gph" ///
			"$path\DK_max_popdens.gph" ///
			"$path\ES_max_popdens.gph" ///
			"$path\FR_max_popdens.gph" ///
			"$path\GE_max_popdens.gph" ///
			"$path\IT_max_popdens.gph" ///
			"$path\NL_max_popdens.gph" ///
			"$path\NO_max_popdens.gph" ///
			"$path\NZ_max_popdens.gph" ///
			"$path\SE_max_popdens.gph" ///
			"$path\UK_max_popdens.gph" ///
			"$path\US_cong_max_popdens.gph" , ///
			col(5) imargin(tiny) scheme (s1mono) xcommon  ///
			title(Comparison of absolute density cutoff points, size(small)) 

graph export  "$path\abs_cutoffs.png" , as(png) replace


* Appendix 1.2: Comparisons of different measures

clear all
cd "$path"
use URED_elections, clear

* drop UK observations from the 60s because of the gap before '92
replace country_code = "UK60" if country_code=="UK" & year<1990

/*****************************************************************************
Figure A1. Urban-rural electoral divides over time for all countries separately,
 based on different choices of cut-off points between urban and rural areas
******************************************************************************/

**** Comparisons of different cut-off points 

keep if country_code == "GE2" | country_code == "IT" | country_code == "NL" | country_code == "ES" | country_code == "SE" | country_code == "UK" | country_code == "FR" | country_code == "CH" | country_code == "AUS" | country_code == "CA" | country_code == "US_cong" | country_code == "NO" | country_code == "NZ" | country_code == "DK" | country_code=="AT"

replace country_code="GE" if country_code=="GE2" // !!!!!!!!!!! = PR vote
replace country="Germany" if country=="Germany2" // !!!!!!!!!!! = PR vote
	
local c_inc "AT AUS CA CH DK ES FR GE IT NL NO NZ SE UK US_cong"
foreach l of local c_inc {
twoway ///
	(connected tot_ur_pol1 	year if country_code=="`l'", lcolor(black) mcolor(black) msize(tiny) lpattern(dot)) ///
	(connected tot_ur_pol 	year if country_code=="`l'", lcolor(black) mcolor(black) msize(tiny) lpattern(shortdash_dot)) /// 
	(connected tot_ur_pol3 	year if country_code=="`l'", lcolor(black) mcolor(black) msize(tiny) lpattern(longdash_dot)) /// 
	(connected tot_ur_pol4 	year if country_code=="`l'", lcolor(black) mcolor(black) msize(tiny) lpattern(dash) msymbol(S)) /// 
	(connected tot_ur_pol5 	year if country_code=="`l'", lcolor(black%50) mcolor(black) msize(tiny) ) /// 
	(connected tot_ur_polw 	year if country_code=="`l'", lcolor(red) mcolor(red) msize(tiny) ) /// 
	,	scheme(s1color) ytitle("") xtitle("") xlab(, angle(45)) ///
		yscale(r(0 0.5)) ylab(0(.1).55) ///
		title("`l' ") ///
		name(el_`l'_c, replace) ///
		legend(	label(1 "top1/bottom1") ///
				label(2 "t2/b2")  ///
				label(3 "t3/b3") ///
				label(4 "t4/b4") /// 
				label(5 "t5/b5") ///
				label(6 "weighted av.")  ///
				col(5)  ///
				) 		 
}


grc1leg el_AUS_c el_AT_c el_CA_c el_DK_c el_FR_c ///
el_IT_c el_CH_c el_ES_c el_GE_c el_NL_c el_NO_c el_NZ_c el_SE_c el_UK_c el_US_cong_c , ///
scheme(s1mono) col(5) imargin(vsmall) ycommon ///
title("Comparisons of measures", size(small)) ///
subtitle("based on different cut-off points", size(vsmall)) name(cutoff_comp, replace) xcommon
graph export  "$path\cutoffs.png" , as(png) replace


* Appendix 2: UK longer term trend 


clear all
cd "$path"
use URED_elections, clear

* drop UK observations from the 60s because of the gap before '92
replace country_code = "UK60" if country_code=="UK" & year<1990

twoway ///
	(connected tot_ur_pol4 year if country_code=="UK60", lcolor(black%50) mcolor(black%50) msize(tiny) msymbol(S) ) /// 
	(connected tot_ur_pol4 year if country_code=="UK", lcolor(black) mcolor(black) msize(tiny) ) /// 
	,	scheme(s1color) ytitle("") xtitle("") xlab(, angle(45)) ///
		title("UK long-term trend ") ///
		name(el_UK_long, replace) ///
		legend(	label(2 "UK 1992-2017") ///
				label(1 "UK 1955-1970") col(2)) 
				
graph export  "$path\UK60.png" , as(png) replace



* Appendix 3: Comparisons across different geographic levels of aggregation

clear all
cd "$path"
use URED_elections, clear



* Germany: Mun., Distr. & electoral Distr.
twoway ///
 (connected tot_ur_pol4 year if country_code=="GE2", lcolor(black) mcolor(black) msize(tiny) msymbol(circle)) ///
 (connected tot_ur_pol4 year if country_code=="GE2_k", lcolor(black) mcolor(black) msize(tiny) msymbol(circle) lpattern(dash)) ///
 (connected tot_ur_pol4 year if country_code=="GE2_wk" & tot_ur_pol4!=0, lcolor(black%50) mcolor(black%50) msize(tiny) msymbol(diamond) lpattern(dot)) ///
 	, 	scheme(s1color) ytitle("U-R electoral divide") xtitle("") ///
		title("Germany") ///
		legend(col(3) stack region(lstyle(none)) ///
		label(1 "Mun.(11k)") ///
		label(2 "Distr.(401)") ///
		label(3 "Elec. distr.(299)") ///
		)	name(germany_comparisons_ur, replace)
		

* France: Mun. and electoral Distr.
	
twoway ///
 (connected tot_ur_pol4 year if country_code=="FR", lcolor(black) mcolor(black) msize(tiny) msymbol(circle)) ///
 (connected tot_ur_pol4 year if country_code=="FR_cst", lcolor(black%50) mcolor(black%50) msize(tiny) msymbol(circle) lpattern(dash)) ///
 	, 	scheme(s1color) ytitle("U-R polarization") xtitle("") ///
		title("France") ///
		legend(col(2) stack region(lstyle(none)) ///
		label(1 "Mun.(35k)") ///
		label(2 "Elec. distr.(577)") ///
		)	name(france_comparisons_parl, replace) 
		
* France: Mun. and electoral Distr.
	
twoway ///
 (connected tot_ur_pol4 year if country_code=="FR2", lcolor(black) mcolor(black) msize(tiny) msymbol(circle)) ///
 (connected tot_ur_pol4 year if country_code=="FR2_cst", lcolor(black%50) mcolor(black%50) msize(tiny) msymbol(circle) lpattern(dash)) ///
 	, 	scheme(s1color) ytitle("U-R polarization") xtitle("") ///
		title("France") ///
		legend(col(2) stack region(lstyle(none)) ///
		label(1 "Mun.(35k)") ///
		label(2 "Elec. distr.(577)") ///
		)	name(france2_comparisons_parl, replace)
		
		

* Switzerland: Mun. and Distr. level
twoway ///
 (connected tot_ur_pol4 year if country_code=="CH", lcolor(black) mcolor(black) msize(tiny) msymbol(circle)) ///
 (connected tot_ur_pol4_cst year if country_code=="CH", lcolor(black%50) mcolor(black%50) msize(tiny) msymbol(circle) lpattern(dash)) ///
 	, 	scheme(s1color) ytitle("U-R polarization") xtitle("") ///
		title("Switzerland") ///
		legend(col(2) stack region(lstyle(none)) ///
		label(1 "Mun.(2.1k)") ///
		label(2 "Distr.(143)") ///
		)	name(CH_comparisons_ur, replace)

		
	

twoway ///
 (connected tot_ur_pol year if country_code=="CH", lcolor(black%50) mcolor(black%50) msize(tiny) msymbol(circle)) ///
 (connected tot_ur_pol2_cst year if country_code=="CH", lcolor(black) mcolor(black) msize(tiny) msymbol(circle) lpattern(dash)) ///
 	, 	scheme(s1color) ytitle("U-R polarization") xtitle("") ///
		title("Switzerland") ///
		legend(col(4) stack region(lstyle(none)) ///
		label(1 "Mun.") ///
		label(2 "Distr.") ///
		)	name(CH_comparisons_ur2, replace)


graph combine CH_comparisons_ur CH_comparisons_ur2, ycommon



* United States: county versus electoral Distr.: presidential elections


twoway ///
 (connected tot_ur_pol4 year if country_code=="US_pres", lcolor(black) mcolor(black) msize(tiny) msymbol(circle)) ///
 (connected tot_ur_pol4 year if country_code=="US_dist", lcolor(black%50) mcolor(black%50) msize(tiny) msymbol(circle) lpattern(dash)) ///
 	, 	scheme(s1color) ytitle("U-R polarization") xtitle("") ///
		title("United States (presidential)") ///
		legend(col(2) stack region(lstyle(none)) ///
		label(1 "County (3k)") ///
		label(2 "Elec. distr.(435)") ///
		)	name(US_comparisons_pres, replace)

graph combine germany_comparisons_ur france_comparisons_parl CH_comparisons_ur US_comparisons_pres, col(2) ycommon xcommon scheme(s1mono)

graph export  "$path\comp_levels.png" , as(png) replace

		
*Appendix 4: Comparison between presidential and legislative in France and the US

clear all
cd "$path"
use URED_elections, clear

* US
twoway ///
 (connected tot_ur_pol4 year if country_code=="US_dist", lcolor(black%50) mcolor(black%50) msize(tiny) msymbol(circle) lpattern(dash)) ///
  (connected tot_ur_pol4 year if country_code=="US_cong", lcolor(black) mcolor(black) msize(tiny) msymbol(circle) ) ///
 	, 	scheme(s1color) ytitle("U-R polarization") xtitle("") ///
		title("United States") ///
		legend(col(2)  region(lstyle(none)) region(lstyle(none)) ///
		label(1 "President") ///
		label(2 "Congress") ///
		)	name(US_cong_pres, replace)
		
	
* FR

twoway ///
 (connected tot_ur_pol4 year if country_code=="FR_pres", lcolor(black) mcolor(black) msize(tiny) msymbol(circle) lpattern(dash)) ///
 (connected tot_ur_pol4 year if country_code=="FR", lcolor(black%50) mcolor(black%50) msize(tiny) msymbol(circle)) ///
 	, 	scheme(s1color) ytitle("U-R polarization") xtitle("") ///
		title("France") ///
		legend(col(1) region(lstyle(none)) ///
		label(1 "President") ///
		label(2 "Parliament") ///
		)	name(FR_pres_parl, replace)
		

* AT
twoway ///
 (connected tot_ur_pol4 year if country_code=="AT_pres", lcolor(black) mcolor(black) msize(tiny) msymbol(circle) lpattern(dash)) ///
 (connected tot_ur_pol4 year if country_code=="AT", lcolor(black%50) mcolor(black%50) msize(tiny) msymbol(circle)) ///
 	, 	scheme(s1color) ytitle("U-R polarization") xtitle("") ///
		title("Austria") ///
		legend(col(1) region(lstyle(none)) ///
		label(1 "President") ///
		label(2 "Parliament") ///
		)	name(AT_pres_parl, replace)		

		
graph combine US_cong_pres FR_pres_parl AT_pres_parl, col(3) ycommon xcommon scheme(s1mono) xsize(*2)
		
graph export  "$path\comp_presleg.png" , as(png) replace wid(5000)
	
* Appendix 5: Comparison between first and second votes in Germany, France and Australia

* France presidential
* 1st round all candidates, 2nd round only 2 candidates
twoway ///
 (connected tot_ur_pol4 year if country_code=="FR_pres", lcolor(black) mcolor(black) msize(tiny) msymbol(circle) lpattern(dash)) ///
 (connected tot_ur_pol4 year if country_code=="FR_pres2", lcolor(black%50) mcolor(black%50) msize(tiny) msymbol(circle)) ///
 	, 	scheme(s1color) ytitle("U-R polarization") xtitle("") ///
		title("France") ///
		legend(col(2) stack region(lstyle(none)) ///
		label(1 "President 1st") ///
		label(2 "President 2nd") ///
		)	name(FR_pres_12, replace)

* France parliamentary
* 1st round in all districts, 2nd round only in more competitive districts?
* What does that comparison actually mean?
twoway ///
 (connected tot_ur_pol4 year if country_code=="FR", lcolor(black) mcolor(black) msize(tiny) msymbol(circle) lpattern(dash)) ///
 (connected tot_ur_pol4 year if country_code=="FR2", lcolor(black%50) mcolor(black%50) msize(tiny) msymbol(circle)) ///
 	, 	scheme(s1color) ytitle("U-R polarization") xtitle("") ///
		title("France") ///
		legend(col(2) stack region(lstyle(none)) ///
		label(1 "Parliament 1st") ///
		label(2 "Parliament 2nd") ///
		)	name(FR_parl_12, replace)
		
twoway ///
 (connected tot_ur_pol4 year if country_code=="FR_pres", lcolor(black) mcolor(black) msize(tiny) msymbol(circle) lpattern(dash)) ///
 (connected tot_ur_pol4 year if country_code=="FR_pres2", lcolor(black%50) mcolor(black%50) msize(tiny) msymbol(circle)) ///
 	, 	scheme(s1color) ytitle("U-R polarization") xtitle("") ///
		title("France") ///
		legend(col(2) stack region(lstyle(none)) ///
		label(1 "President 1st") ///
		label(2 "President 2nd") ///
		)	name(FR_pres_12, replace)
		
		
* Germany 1st and 2nd vote 
twoway ///
 (connected tot_ur_pol4 year if country_code=="GE2", lcolor(black) mcolor(black) msize(tiny) msymbol(circle) lpattern(dash)) ///
 (connected tot_ur_pol4 year if country_code=="GE", lcolor(black%50) mcolor(black%50) msize(tiny) msymbol(circle)) ///
 	, 	scheme(s1color) ytitle("U-R polarization") xtitle("") ///
		title("Germany") ///
		legend(col(2) stack region(lstyle(none)) ///
		label(1 "Proportional representation") ///
		label(2 "Single-member district") ///
		)	name(GE_parl_12, replace)

		
* Australia first-preference and two-party-preference
twoway ///
 (connected tot_ur_pol4 year if country_code=="AUS", lcolor(black) mcolor(black) msize(tiny) msymbol(circle) lpattern(dash)) ///
 (connected tot_ur_pol4 year if country_code=="AUS2", lcolor(black%50) mcolor(black%50) msize(tiny) msymbol(circle)) ///
 	, 	scheme(s1color) ytitle("U-R polarization") xtitle("") ///
		title("Australia") ///
		legend(col(2) stack region(lstyle(none)) ///
		label(1 "First preference") ///
		label(2 "Two-candidate preferred") ///
		)	name(AUS_parl_12, replace)
		

graph combine 		FR_pres_12 FR_parl_12 GE_parl_12 AUS_parl_12, col(2) ycommon xcommon scheme(s1mono)

graph export  "$path\comp_votes.png" , as(png) replace















/*

/*----------------------------------------------------------------------------
NOTE:
	The code below produces the maps in Appendix 1. However, these maps were 
	made with the country-specific data files before they were aggregated into
	URED_regions. Moreover, additional shapefiles need to be merged with these 
	country-specific files, as is done in the code below. Most shapefiles can 
	be found through the links in Table 1 of the codebook.
	
	If you want to replicate these maps and the links in the codebook plus 
	the code below are not sufficient, please do get in touch.

		
*** Follow-up Appendix 1: 

*** Appendix 1.3 maps of urban/rural classification 

*/

**************** Australia **************** 
cd "$path\Data\AUS"
* get shapefile data 
spshape2dta "$path\Data\AUS\2021_ELB_region.shp", replace


use "$path\Data\AUS\2021_ELB_region_shp", clear 

replace _X=115 if _X<110
replace _X=160 if _X>160

save "$path\Data\AUS\2021_ELB_region_shp", replace


use "$path\Data\AUS\2021_ELB_region.dta", clear
rename Elect_div regionname
gen year=2022
keep _ID _CX _CY regionname year Area_SqKm
replace regionname = "Fraser (VIC)" if regionname=="Fraser"
save AUS_shp_2022.dta, replace

* merge with election data 
use "$path\Data\AUS\cyr_AUS", clear
drop _merge
merge m:1 year regionname using AUS_shp_2022
keep if _merge==3
keep if pickone_cyr

keep country country_code year region_code popdens urban10 validvote regionname totalvote electorate area_id _ID _CX _CY Area_SqKm popdens

* add city labels 
gen city_label = ""
replace city_label = "SY" if _ID==1
replace city_label = "ME" if _ID==126
replace city_label = "BR" if _ID==56
replace city_label = "PE" if _ID==149
replace city_label = "AD" if _ID==83


save "cyr_AUS_shp22.dta", replace


use cyr_AUS_shp22, clear 

spmap urban10 using "$path\Data\AUS\2021_ELB_region_shp", id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(black green%20 black%20 red%50 red) ///
title("Australia (2022)", size(medium)) ///
legend(col(6)  colgap(*.5)  size(medium) ///
label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")) legorder(lohi) legstyle(2) ///
legtitle("Population density decile")  ///
name(map_AUS22, replace) ///
label(data("cyr_AUS_shp22") xcoord(_CX) ycoord(_CY)  color(black) label(city_label) pos(3)   ///
select(keep if city_label!="") ) ///
point(data("cyr_AUS_shp22") xcoord(_CX) ycoord(_CY)  fcolor(red) ocolor(red)  ///
legenda(off) select(keep if city_label!="") ) ///
fysize(75)

levelsof area_id, local(levels)
foreach x of local levels {
spmap urban10 using "$path\Data\AUS\2021_ELB_region_shp" if area_id==`x', id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(black green%20 black%20 red%50 red) ///
title("Australia `x' (2022)") ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5)  size(medium)) legorder(lohi) legstyle(2) ///
legtitle("Population density decile") legjunction(" to ") ///
name(map_AUS22_`x', replace) ///
label(data(cyr_AUS_shp22) xcoord(_CX)  ycoord(_CY) select(keep if area_id==`x') ///
  label(_ID) size(*0.85 ..) pos(12 0) )
}

*2. Sydney
spmap urban10 using "$path\Data\AUS\2021_ELB_region_shp" if area_id==2 & urban10>4 & (_ID!=38 & _ID!=20 & _ID!=16 & _ID!=10 & _ID!=42 & _ID!=12 & _ID!=41 & _ID!=11 & _ID!=31 & _ID!=25 & _ID!=23 & _ID!=26 & _ID!=25) , id(_ID) ///
osize(thin thin thin thin thin thin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
title("Sydney") ///
name(map_AUS22_2, replace) ///
ndf(yellow%10) ndo(black%20) ///
 fysize(25)


 *4. Brisbane
spmap urban10 using "$path\Data\AUS\2021_ELB_region_shp" if area_id==4 & urban10>6 & (_ID!=75 & _ID!=68 & _ID!=67 & _ID!=66) , id(_ID) ///
osize(thin thin thin thin thin thin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
title("Brisbane") ///
name(map_AUS22_4, replace) ///
ndf(yellow%10) ndo(black%10) ///
 fysize(25)

 *5. Adelaide
spmap urban10 using "$path\Data\AUS\2021_ELB_region_shp" if area_id==5 & urban10>4 & (_ID!=85 & _ID!=84 ) , id(_ID) ///
osize(thin thin thin thin thin thin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
title("Adelaide") ///
name(map_AUS22_5, replace) ///
ndf(yellow%10) ndo(black%10)  fysize(25)

 *7. Melbourne
spmap urban10 using "$path\Data\AUS\2021_ELB_region_shp" if area_id==7 & urban10>4 & (_ID!=100 & _ID!=107 & _ID!=125 & _ID!=118 & _ID!=115 & _ID!=102 &_ID!=132 ) , id(_ID) ///
osize(thin thin thin thin thin thin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
title("Melbourne") ///
name(map_AUS22_7, replace) ///
ndf(yellow%10) ndo(black%10) ///
fysize(25)


 *8. Perth
spmap urban10 using "$path\Data\AUS\2021_ELB_region_shp" if area_id==8 & urban10>4 & _ID!=143 & _ID!=144 & _ID!=137 & _ID!=148 , id(_ID) ///
osize(thin thin thin thin thin thin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
title("Perth") ///
name(map_AUS22_8, replace) ///
ndf(yellow%10) ndo(black%10) ///
 fysize(25)


grc1leg  map_AUS22_2 map_AUS22_4 map_AUS22_5 map_AUS22_7 map_AUS22_8, ///
name(AUS_cities, replace) fysize(25) col(5)
 
grc1leg  map_AUS22 AUS_cities, imargin(0) col(1) scheme(s1mono) ///
fxsize(50) fysize(50) 

graph save "$path\\map_AUS.gph", replace

**************** Austria **************** 
use "$path\Data\AT\cyr_AT", clear

keep if pickone_cyr & year==2019
spmap urban10 using "$path\Data\AT\at_gem2019_wgs_shp", id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
title("Austria (2019)") ///
ocolor(green%50 green%20 black%20 red%50 red) ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5) size(medium)) legorder(lohi) legstyle(2) ///
legtitle("Population density decile") legjunction(" to ") ///
ndf(yellow%10) ndo(black%10)

graph save "$path\\map_AT.gph", replace

**************** Canada **************** 
cd "$path\Data\CA"


* get shapefile data 
spshape2dta "$path\Data\CA\FED_CA_2021_EN.shp", replace
/*
use FED_CA_2021_EN_shp, clear
replace _Y=3539818 if _Y>3539818
save FED_CA_2021_EN_shp, replace
*/
use "$path\Data\CA\cyr_CA", clear

keep if year==2021 & pickone_cyr
gen FED_NUM = substr(region_code, 5, 9)
destring FED_NUM, replace
drop _merge

merge 1:m FED_NUM using FED_CA_2021_EN.dta
drop _merge


* add city labels 
gen city_label = ""
replace city_label = "TO" if _ID==323
replace city_label = "MO" if _ID==206
*replace city_label = "VI" if _ID==140
replace city_label = "VA" if _ID==46
replace city_label = "OT" if _ID==52
replace city_label = "QU" if _ID==34
replace city_label = "ED" if _ID==167
replace city_label = "CA" if _ID==125

* total votes, for points proportional to size 
gen city_total_vote = .
replace city_total_vote = totalvote/1000 if city_label!=""

save cyr_CA_shp, replace

spmap urban10 using "$path\Data\CA\FED_CA_2021_EN_shp" , id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(black green%20 black%20 red%50 red) ///
title("Canada (2019)", size(medium)) ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5) size(medium) pos(12)) legorder(lohi) legstyle(2)  ///
legtitle("Population density decile") legjunction(" to ") ///
name(map_ca19, replace) ///
ndf(yellow%10) ndo(black%10)



foreach x of numlist 1 2 3 9 11 12 {
spmap urban10 using "$path\Data\CA\FED_CA_2021_EN_shp" if area_id==`x' & urban10>2 , id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(black green%20 black%20 red%50 red) ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5)  size(medium)) legorder(lohi) legstyle(2) ///
legtitle("Population density decile") legjunction(" to ") ///
name(map_CA21_`x', replace) ///
label(data(cyr_CA_shp) xcoord(_CX)  ycoord(_CY) select(keep if area_id==`x' & urban10>2) ///
  label(_ID) size(*0.85 ..)  )
}


*1. 

*a. Edmonton 
spmap urban10 using "$path\Data\CA\FED_CA_2021_EN_shp" if area_id==1 & urban10>4 & _CY>2074949.7 , id(_ID) ///
osize(thin thin thin thin thin thin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5)  size(medium)) legorder(lohi) legstyle(2) ///
title("Edmonton") ///
legtitle("Population density decile") legjunction(" to ") ///
name(map_CA21_1a, replace)  fysize(25) 
*fxsize(25)



*b. Calgary
spmap urban10 using "$path\Data\CA\FED_CA_2021_EN_shp" if area_id==1 & urban10>4 & _CY<2074949.7 , id(_ID) ///
osize(thin thin thin thin thin thin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
title("Calgary") ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5)  size(medium)) legorder(lohi) legstyle(2) ///
legtitle("Population density decile") legjunction(" to ") ///
name(map_CA21_1b, replace) fysize(25) 
*fxsize(25)


*2. Vancouver
spmap urban10 using "$path\Data\CA\FED_CA_2021_EN_shp" if area_id==2 & _ID!=170 & _ID!=171 & _ID!=274 & _ID!=168 & _ID!=46 & _ID!=208 & _ID!=169 & _ID!=62 & _ID!=275 & _ID!=85 & urban10>4 & _CY>1966247.9, id(_ID) ///
osize(thin thin thin thin thin thin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
title("Vancouver") ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5)  size(medium)) legorder(lohi) legstyle(2) ///
legtitle("Population density decile") legjunction(" to ") ///
name(map_CA21_2, replace) ///
fysize(25)




*3. Winnipeg
spmap urban10 using "$path\Data\CA\FED_CA_2021_EN_shp" if area_id==3 & urban10>4 , id(_ID) ///
osize(thin thin thin thin thin thin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
title("Winnipeg") ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5)  size(medium)) legorder(lohi) legstyle(2) ///
legtitle("Population density decile") legjunction(" to ") ///
name(map_CA21_3, replace) fysize(25) 
*fxsize(25)

*9. 
*a. Ottawa
spmap urban10 using "$path\Data\CA\FED_CA_2021_EN_shp" if area_id==9 & _CY>1043464.4 & (urban10>4 | _ID==315) , id(_ID) ///
osize(thin thin thin thin thin thin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
title("Ottawa") ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5)  size(medium)) legorder(lohi) legstyle(2) ///
legtitle("Population density decile") legjunction(" to ") ///
name(map_CA21_9a, replace) fysize(25) 
*fxsize(25)

*b. Toronto
spmap urban10 using "$path\Data\CA\FED_CA_2021_EN_shp" if area_id==9 & _CY<1043464.4 & _CY>846559.2 & (urban10>6 | _ID==234 | _ID==70 | _ID==300)& _ID!=313 & _ID!=57 & _ID!=321 & _ID!=292 & _ID!=68 & _ID!=31 & _ID!=316 ///
, id(_ID) ///
osize(vthin vthin vthin vthin vthin vthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
title("Toronto") ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5)  size(medium)) legorder(lohi) legstyle(2) ///
legtitle("Population density decile") legjunction(" to ") ///
name(map_CA21_9b, replace) ///
fysize(25)  



*11
*a. Quebec
spmap urban10 using "$path\Data\CA\FED_CA_2021_EN_shp" if area_id==11 & _CY>1358669.5 & urban10>4 & _ID!=41 & _ID!=25 ///
, id(_ID) ///
osize(thin thin thin thin thin thin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
title("Quebec") ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5)  size(medium)) legorder(lohi) legstyle(2) ///
legtitle("Population density decile") legjunction(" to ") ///
name(map_CA21_11a, replace) fysize(25) 
*fxsize(25)

*b. Montreal 
spmap urban10 using "$path\Data\CA\FED_CA_2021_EN_shp" if area_id==11 & _CY<1358669.5 & urban10>6 & _ID!=32 & _ID!=20 & _ID!=73 & _ID!=77 ///
, id(_ID) ///
osize(thin thin thin thin thin thin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
title("Montreal") ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5)  size(medium)) legorder(lohi) legstyle(2) ///
legtitle("Population density decile") legjunction(" to ") ///
name(map_CA21_11b, replace) fysize(25)



grc1leg , imargin(zero) scheme(s1mono) col(2) name(ca_cities_2, replace) fysize(25) fxsize(25)

grc1leg map_CA21_9b map_CA21_11b map_CA21_2 ca_cities_2, imargin(zero) scheme(s1mono) col(4) name(ca_cities_1, replace) fysize(25)

grc1leg map_CA21_9b map_CA21_11b map_CA21_2 map_CA21_9a map_CA21_11a map_CA21_1a map_CA21_1b, imargin(zero) scheme(s1mono) col(7) name(ca_cities, replace) fysize(25)

use FED_CA_2021_EN_shp, clear
replace _Y=3539818 if _Y>3539818
save FED_CA_2021_EN_shp2, replace

use "$path\Data\CA\cyr_CA_shp", clear


spmap urban10 using "$path\Data\CA\FED_CA_2021_EN_shp2" , id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(black%50 black%50 black%20 red%50 red) ///
title("Canada (2019)", size(medium)) ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5) size(medium) pos(12)) legorder(lohi) legstyle(2)  ///
legtitle("Population density decile") legjunction(" to ") ///
name(map_ca19, replace) ///
ndf(yellow%10) ndo(black%10) ///
label(data("cyr_CA_shp") xcoord(_CX) ycoord(_CY)  color(black) label(city_label) pos(3) angle(335) ///
select(keep if city_label!="") ) ///
point(data("cyr_CA_shp") xcoord(_CX) ycoord(_CY)  fcolor(red) ocolor(red)  ///
legenda(off) select(keep if city_label!="") ) ///
fysize(75) 

 

  
grc1leg map_ca19 ca_cities, col(1) scheme(s1mono) imargin(zero) name(map_CA, replace) fysize(50) fxsize(50) 
graph save "$path\\map_CA.gph", replace


**************** Switzerland **************** 
cd "$path\Data\CH"
* get shapefile data 
spshape2dta "$path\Data\CH\2019_THK_PRO\PRO\01_INST\Gesamtfläche_gf\K4_polg20190101_gf\K4polg20190101gf_ch2007Poly.shp", replace
use K4polg20190101gf_ch2007Poly.dta, clear

use cyr_CH, clear
drop _merge
destring region_code, gen(GDENR)
keep if pickone_cyr & year==2019
merge 1:1 GDENR using K4polg20190101gf_ch2007Poly

tab regionname _merge if _merge!=3
table GDENAME if _merge!=3, contents(mean _ID)

replace _ID=1127 if regionname=="Bagnes"
replace _ID=652 if regionname=="Gänsbrunnen"
replace _ID=1975 if regionname=="Brione (Verzasca)"
replace _ID=533 if regionname=="Kirchenthurnen"
replace _ID=1946 if regionname=="Mühlethurnen"
replace _ID=19 if regionname=="Prez-vers-Noréaz"
replace _ID=2143 if regionname=="Villaz-Saint-Pierre"
replace _ID=1578 if regionname=="Welschenrohr"

bysort _ID: egen _CX2=mean(_CX)
bysort _ID: egen _CY2=mean(_CY)

replace _CX = _CX2 if _CX==.
replace _CY = _CY2 if _CY==.

duplicates tag _ID, gen(duptag)
drop if duptag==1 & regionname==""

drop _merge _CX2 _CY2
sort _ID

save cyr_CH_id, replace 

spmap urban10 using "$path\Data\CH\K4polg20190101gf_ch2007Poly_shp" , id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) ocolor(black) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
title("Switzerland (2019)") ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5) size(medium) pos(12)) legorder(lohi) legstyle(2)  ///
legtitle("Population density decile") legjunction(" to ") ///
ndf(yellow%10) ndo(black%10)
graph save "$path\\map_CH.gph", replace



spshape2dta "$path\Data\CH\2019_THK_PRO\PRO\01_INST\Gesamtfläche_gf\K4_polg20190101_gf\K4polg20190101gf_ch2007Pnts.shp", replace

use K4polg20190101gf_ch2007Pnts, clear 
rename _ID _ID2 
rename _CX _CX2 
rename _CY _CY2 


save K4polg20190101gf_ch2007Pnts, replace 

use K4polg20190101gf_ch2007Pnts_shp, clear 
rename _ID _ID2 


save K4polg20190101gf_ch2007Pnts_shp, replace 

use cyr_CH_id, clear 
merge 1:1 GDENR using K4polg20190101gf_ch2007Pnts



tab regionname _merge if _merge!=3
table GDENAME if _merge!=3, contents(mean _ID2)

replace _ID2=2128 if regionname=="Bagnes"
replace _ID2= 1034 if  regionname=="Gänsbrunnen"
replace _ID2=1809 if regionname=="Brione (Verzasca)"
replace _ID2=361 if regionname=="Kirchenthurnen"
replace _ID2=359 if regionname=="Mühlethurnen"
replace _ID2=630 if regionname=="Prez-vers-Noréaz"
replace _ID2=860 if regionname=="Villaz-Saint-Pierre"
replace _ID2=1018 if regionname=="Welschenrohr"



bysort _ID2: egen _CX22=mean(_CX2)
bysort _ID2: egen _CY22=mean(_CY2)

replace _CX2 = _CX22 if _CX2==.
replace _CY2 = _CY22 if _CY2==.

duplicates tag _ID2, gen(duptag2)
drop if duptag2==1 & regionname==""

drop _merge _CX22 _CY22
sort _ID2

save cyr_CH_id_pnts, replace 



use K4polg20190101gf_ch2007Pnts, clear


merge 1:1 _ID2 using cyr_CH_id_pnts
keep _ID2 _CX _CY GDENR GDENAME _ID2 _CX _CY GDENR GDENAME popdens urban10 totalvote
save K4polg20190101gf_ch2007Pnts.dta, replace


use cyr_CH_id, clear 

spmap urban10 using "$path\Data\CH\K4polg20190101gf_ch2007Poly_shp" , id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(green%50 green%20 black%20 red%50 red) ///
title("Switzerland (2019)") ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5) size(medium) pos(6)) legorder(lohi) legstyle(2)  ///
legtitle("Population density decile") legjunction(" to ") ///
ndf(yellow%10) ndo(black%10)  name(CH_mun, replace)






**************** Denmark **************** 
cd "$path\Data\DK"


use cyr_DK, clear
gen _ID = region_code - 99000
drop _merge
merge m:1 _ID using DK2015_geo

spmap urban10 using "$path\Data\DK\DK2015_geo_coor" if year==2022 & pickone_cyr, id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(black%50 black%20 black%20 red%50 red) ///
title("Denmark (2022)") ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5) size(medium) pos(12)) legorder(lohi) legstyle(2)  ///
legtitle("Population density decile") legjunction(" to ") ///
ndf(yellow%10) ndo(black%10) 

graph save "$path\\map_DK.gph", replace


**************** Spain **************** 
cd "$path\Data\ES"
* get shapefile data 
spshape2dta "$path\Data\ES\georef-spain-municipio-millesime.shp", replace

* relocate canarian islands closer to main land
use georef-spain-municipio-millesime_shp.dta, clear

replace _X = _X+5 if _Y<30
replace _Y = _Y+10 if _Y<30

save georef-spain-municipio-millesime_shp_rec.dta, replace

use cyr_ES.dta, replace
keep if pickone_cyr
keep if year == 2020
drop year

gen mun_code = substr(region_code, 5, 9)
drop _merge
merge 1:1 mun_code using georef-spain-municipio-millesime.dta

spmap urban10 using "$path\Data\ES\georef-spain-municipio-millesime_shp" if _CY>30 , id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(green%50 green%20 black%20 red%50 red) ///
title("Spain (2020)", size(medium)) ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5) size(medium) pos(6)) legorder(lohi) legstyle(2)  ///
legtitle("Population density decile") legjunction(" to ") ///
ndf(yellow%10) ndo(black%10) 

graph save "$path\\map_ES.gph", replace

**************** France **************** 
cd "$path\Data\FR"

use cyr_FR, clear
drop _merge 
keep if pickone_cyr & year==2022
save cyr_FR_2022.dta, replace

use commune2016, clear 

gen region_code = CODE_DEPT + CODE_COM

merge 1:1 region_code using cyr_FR_2022

tab regionname if _merge==2


replace region_code = "75056" if (strpos(NOM_COM, "PARIS-") & strpos(NOM_COM, "ARRONDISSEMENT"))
replace region_code = "69123" if (strpos(NOM_COM, "LYON-") & strpos(NOM_COM, "ARRONDISSEMENT"))
replace region_code = "13055" if (strpos(NOM_COM, "MARSEILLE-") & strpos(NOM_COM, "ARRONDISSEMENT"))

bysort region_code: egen urban10_2 = mean(urban10) 

replace urban10=urban10_2 if urban10==.
drop urban10_2

duplicates tag _ID, gen(duptag)
drop if duptag & NOM_COM==""

spmap urban10 using "$path\Data\FR\commune2016_shp" , id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin)  clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(green%50 green%20 black%20 red%50 red) ///
title("France (2022)", size(medium)) ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5) size(medium) pos(6)) legorder(lohi) legstyle(2)  ///
legtitle("Population density decile") legjunction(" to ") ///
ndf(yellow%10) ndo(black%10) name(FR_mun, replace)

graph save "$path\\map_FR.gph", replace


**************** Germany **************** 
cd "$path\Data\GE"
* get shapefile data 
*https://daten.gdz.bkg.bund.de/produkte/vg/vg5000_1231/2021/
spshape2dta "$path\Data\GE\G-2020-AI002-1-5--AI0201--2022-11-26.shp", replace

use G-2020-AI002-1-5--AI0201--2022-11-26, clear


spshape2dta "VG5000_GEM.shp", replace

use cyr_GE.dta, clear
keep if year==2021 & pickone_cyr
gen AGS = region_code 

drop _merge

merge 1:1 AGS using VG5000_GEM

replace urban10=0 if BEZ=="Gemeindefreies Gebiet"

spmap urban10 using "$path\Data\GE\VG5000_GEM_shp" , id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(green%50 green%20 black%20 red%50 red) ///
title("Germany (2021)", size(medium)) ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5) size(medium) pos(6)) legorder(lohi) legstyle(2)  ///
legtitle("Population density decile") legjunction(" to ") ///
ndf(yellow%10) ndo(black%10) 

graph save "$path\\map_GE.gph", replace


**************** Italy **************** 
cd "$path\Data\IT"
* get shapefile data 
spshape2dta "$path\Data\IT\Com01012022_g_WGS84.shp", replace

use cyr_IT.dta, clear

keep if pickone_cyr & year==2022
list _ID regionname region_code if regionname=="roma" | regionname=="torino"
codebook _ID


spmap urban10 using "$path\Data\IT\Com01012022_g_WGS84_shp" , id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(green%50 green%20 black%20 red%50 red) ///
title("Italy (2022)", size(medium)) ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5) size(medium) pos(6)) legorder(lohi) legstyle(2)  ///
legtitle("Population density decile") legjunction(" to ") ///
ndf(yellow%10) ndo(black%10) 

graph save "$path\\map_IT.gph", replace



**************** Netherlands**************** 
cd "$path\Data\NL"
* get shapefile data 
spshape2dta "$path\Data\NL\gemeente_2021_v1.shp", replace

use gemeente_2021_v1, clear
drop if H2O=="JA"
save gemeente_2021_v2.dta, replace

use cyr_NL, clear
keep if pickone_cyr & year==2021

gen GM_CODE = subinstr(region_code,"NLG","GM",.)

drop _merge
merge 1:1 GM_CODE using gemeente_2021_v2
drop if _merge==2 

spmap urban10 using "$path\Data\NL\gemeente_2021_v1_shp" , id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(green%50 green%20 black%20 red%50 red) ///
title("Netherlands (2021)") ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5) size(medium) pos(6)) legorder(lohi) legstyle(2)  ///
legtitle("Population density decile") legjunction(" to ") ///
ndf(yellow%10) ndo(black%10) ///
fysize(25) fxsize(20)

graph save "$path\\map_NL.gph", replace


**************** Norway **************** 
cd "$path\Data\NO"

* get shapefile data 
spshape2dta "$path\Data\NO\Hist_kommune_flate_2017.shp", replace

use Hist_kommune_flate_2017, clear
destring KOMMUNENR, gen(region_code)
save Hist_kommune_flate_2017.dta, replace

use cyr_NO.dta, clear
tab year
keep if pickone_cyr & year==2013
drop _merge
merge 1:1 region_code using Hist_kommune_flate_2017


spmap urban10 using "$path\Data\NO\Hist_kommune_flate_2017_shp" , id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(black%50 black%20 black%20 red%50 red) ///
title("Norway (2013)", size(medium)) ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5) size(medium) pos(6)) legorder(lohi) legstyle(2)  ///
legtitle("Population density decile") legjunction(" to ") ///
ndf(yellow%10) ndo(black%10) 

graph save "$path\\map_NO.gph", replace

**************** New Zealand **************** 
cd "$path\Data\NZ"

spshape2dta "general-electorates-2020.shp", replace


use general-electorates-2020_shp, clear 
replace _X = _X*-1 if _ID==43 & _X<0
save general-electorates-2020_shp, replace

use cyr_NZ, clear
keep if pickone_cyr & year==2020



spmap urban10 using "$path\Data\NZ\general-electorates-2020_shp" , id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin)  clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(black black black%20 red%50 red) ///
title("New Zealand (2020)") ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5) size(medium) pos(6)) legorder(lohi) legstyle(2)  ///
legtitle("Population density decile") legjunction(" to ") ///
ndf(yellow%10) ndo(black%10)  ///
name(map_nz20, replace) fxsize(50) fysize(50) 

graph save "$path\\map_NZ.gph", replace


**************** Sweden **************** 
cd "$path\Data\SE"

spshape2dta "SWE_adm2.shp", replace

use SWE_adm2, clear

use cyr_SE, clear
order regionname region_code
keep if pickone_cyr 
tab year
keep if year==2022

gen NAME_2 = regionname 

replace NAME_2 = "Malung" if regionname =="Malung-Sälen"
replace NAME_2 = "Upplands-Väsby" if regionname =="Upplands Väsby"
drop _merge
merge 1:1 NAME_2 using SWE_adm2


spmap urban10 using "$path\Data\SE\SWE_adm2_shp" , id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(black%50 black%20 black%20 red%50 red) ///
title("Sweden (2022)", size(medium)) ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5) size(medium) pos(6)) legorder(lohi) legstyle(2)  ///
legtitle("Population density decile") legjunction(" to ") ///
ndf(yellow%10) ndo(black%10)

graph save "$path\\map_SE.gph", replace

**************** United Kindom **************** 
cd "$path\Data\UK"

use cyr_UK, clear
keep if pickone_cyr
tab year
keep if year==2019


spmap urban10 using "$path\Data\UK\shapefiles\GB_cst_2011_coor" , id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(black%50 black%50 black%20 red%50 red) ///
title("United Kingdom (2019)", size(medium)) subtitle(England & Scotland, size(small)) ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5) size(medium) pos(6)) legorder(lohi) legstyle(2)  ///
legtitle("Population density decile") legjunction(" to ") ///
ndf(yellow%10) ndo(black%10) ///
name(map_uk19, replace) fxsize(50) fysize(50) 

graph save "$path\\map_UK.gph", replace

 
**************** United States**************** 
cd "$path\Data\US"

use cyr_US_congress, clear

keep if pickone_cyr
tab year
keep if year==2020

drop if statename=="Alaska" | statename=="Hawaii"

keep _ID regionname year urban10 popdens statename GEOID
*save US_map20.dta, replace
*export excel using "US_map20", replace first(var)


spmap urban10 using "$path\Data\US\cd_116_coor" , id(_ID) ///
osize(vvthin vvthin vvthin vvthin vvthin vvthin) clm(custom) clbreaks(0 2 4 6 8 10) ///
fcolor(green%50 green%20 black%20 red%50 red) ///
ocolor(black%50 black%50 black%20 red%50 red) ///
title("United States (2020)", size(medium)) ///
legend(col(6) label(2 "1,2") label(3 "3,4") label(4 "5,6") label(5 "7,8") label(6 "9,10")  colgap(*.5) size(medium) pos(7)) legorder(lohi) legstyle(2)  ///
legtitle("Population density decile") legjunction(" to ") ///
ndf(yellow%20) ndo(black%10) ///
note("Note: Districts in Alaska & Hawaii not displayed", size(small)) ///
name(map_us20, replace) 

graph save "$path\map_US.gph", replace
 

 *********** combine maps 
 cd "$path\"
 
  
* 5 Anglo-saxon countries 

grc1leg map_UK.gph map_NZ.gph, scheme(s1mono) col(2)  name(map_UK_NZ, replace) 
  
grc1leg map_US.gph map_CA.gph map_UK_NZ map_AUS.gph, scheme(s1mono) col(2) ysize(8) xsize(11)
graph save map_anglo4_base.gph, replace 

* Europe
grc1leg map_NL.gph map_CH.gph, scheme(s1mono) col(1) fxsize(20) fysize(50) name(map_NL_CH, replace) imargin(zero)
grc1leg map_AT.gph map_DK.gph, scheme(s1mono) col(1) name(map_AT_DK, replace) imargin(zero)

grc1leg map_IT.gph map_SE.gph map_NO.gph map_AT_DK map_NL_CH , col(5) scheme(s1mono) imargin(zero) fysize(50) name(map_EU_long, replace)
grc1leg map_GE.gph map_FR.gph map_ES.gph, col(3) scheme(s1mono) imargin(zero) fysize(50) name(map_EU_wide, replace)

grc1leg map_EU_wide map_EU_long, col(1) scheme(s1mono) imargin(zero)






************ France comparison constituencies and municipalities 


*** constituency shapefile
spshape2dta "$path\Data\FR\shapefiles\fr_cst2010.shp", replace
use fr_cst2010, clear

drop if _CY<20
drop if _CX<-40
spmap  using fr_cst2010_shp , id(_ID) 
save fr_cst, replace 

use fr_cst2010_shp, clear 
drop if _Y<20
drop if _X<-40
save fr_cst2010_shp, replace 
*note: made coords ref systems comparable in R: harmonise_shp.R

*** municipality shapefile 